package com.inei.cie.capitulovi;

import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import com.inei.cie.Carga_Trabajo_Activity;
import com.inei.cie.R;
import com.inei.cie.capituloix.CapituloIX_Activity;
import com.inei.cie.dao.CapituloVIDao;
import com.inei.cie.entidades.P6_2;
import com.inei.cie.entidades.P6_2_4N;

public class CapituloVI_SeccionB_Evaluacion_De_Los_SSHH_Activity extends
		Activity {

	public TextView tvEdificacionNro;
	public int numero;
	public String edificacion;
	public int Nro_Ed;
	public int P6_2_1;
	public int Nro_Pred;
	public int P6_2_2;
	public String id_local;
	public P6_2 obj2;
	public List<P6_2_4N> lObj2;
	public CapituloVIDao cap6dao;

	public EditText et6B14turcos, et6B14letrinas, et6B14inodorosa1,
			et6B14inodorosa2, et6B14inodorosn1, et6B14inodorosn2, et6B14Atotal,
			et6B141, et6B142;
	public TableLayout tl6Bevaluacion;
	public TableRow tr6B14, tr6B14A, tr6B14B;
	public Spinner sp6Btipo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.capitulo_vi_seccion_b_evaluacion_de_los_sshh);

		IniciarComponentes();

		et6B14Atotal.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence c, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				if (!c.toString().equals("")) {
					int num1 = Integer.parseInt(et6B14inodorosa1.getText()
							.toString().length() == 0 ? "0" : et6B14inodorosa1
							.getText().toString());
					int num2 = Integer.parseInt(et6B14inodorosa2.getText()
							.toString().length() == 0 ? "0" : et6B14inodorosa2
							.getText().toString());
					int num3 = Integer.parseInt(et6B14inodorosn1.getText()
							.toString().length() == 0 ? "0" : et6B14inodorosn1
							.getText().toString());
					int num4 = Integer.parseInt(et6B14inodorosn2.getText()
							.toString().length() == 0 ? "0" : et6B14inodorosn2
							.getText().toString());
					int total = Integer.parseInt(et6B14Atotal.getText()
							.toString().length() == 0 ? "0" : et6B14Atotal
							.getText().toString());
					if (total > (num1 + num2 + num3 + num4)) {
						et6B14Atotal.setText("");
						Toast.makeText(
								getApplicationContext(),
								"El valor ingresado debe ser menor o igual al total de inodoros",
								Toast.LENGTH_SHORT).show();
					}
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});

		et6B142.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence c, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				if (!c.toString().equals("")) {
					int num1 = Integer
							.parseInt(et6B141.getText().toString().length() == 0 ? "0"
									: et6B141.getText().toString());
					int num2 = Integer
							.parseInt(et6B142.getText().toString().length() == 0 ? "0"
									: et6B142.getText().toString());
					if (num2 > num1) {
						et6B142.setText("");
						Toast.makeText(
								getApplicationContext(),
								"El valor ingresado debe ser menor o igual al total de lavaderos o lavatorios",
								Toast.LENGTH_SHORT).show();
					}
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});

	}

	public void IniciarComponentes() {
		id_local = getIntent().getExtras().getString("id_local");
		Nro_Ed = getIntent().getExtras().getInt("Nro_Ed");
		P6_2_1 = getIntent().getExtras().getInt("P6_2_1");
		P6_2_2 = getIntent().getExtras().getInt("P6_2_2");
		Nro_Pred = getIntent().getExtras().getInt("Nro_Pred");
		edificacion = "Local : "
				+ id_local
				+ " - Predio : "
				+ (getIntent().getExtras().getInt("TITULO_PRED") < 10 ? "0"
						: "") + getIntent().getExtras().getInt("TITULO_PRED")
				+ " - Edificaci\u00f3n Nro. " + (Nro_Ed < 10 ? "0" : "")
				+ Nro_Ed;
		tvEdificacionNro = (TextView) findViewById(R.id.tvEdificacionNro);
		tvEdificacionNro.setText(edificacion);

		tl6Bevaluacion = (TableLayout) findViewById(R.id.tl6Bevaluacion);
		tr6B14 = (TableRow) findViewById(R.id.tr6B14);
		tr6B14A = (TableRow) findViewById(R.id.tr6B14A);
		tr6B14B = (TableRow) findViewById(R.id.tr6B14B);

		et6B14turcos = (EditText) findViewById(R.id.et6B14turcos);
		et6B14letrinas = (EditText) findViewById(R.id.et6B14letrinas);
		et6B14inodorosa1 = (EditText) findViewById(R.id.et6B14inodorosa1);
		et6B14inodorosa2 = (EditText) findViewById(R.id.et6B14inodorosa2);
		et6B14inodorosn1 = (EditText) findViewById(R.id.et6B14inodorosn1);
		et6B14inodorosn2 = (EditText) findViewById(R.id.et6B14inodorosn2);
		et6B14Atotal = (EditText) findViewById(R.id.et6B14Atotal);
		et6B141 = (EditText) findViewById(R.id.et6B141);
		et6B142 = (EditText) findViewById(R.id.et6B142);

		CargarDatos();
	}

	public void CargarDatos() {
		cap6dao = new CapituloVIDao(getApplicationContext());
		List<Object> lista = cap6dao.obtenerAmbiente(id_local, Nro_Ed,
				Nro_Pred, P6_2_1, P6_2_2);
		obj2 = (P6_2) lista.get(0);
		lObj2 = (List<P6_2_4N>) lista.get(1);

		if (obj2.getP6_2_14_1() != 0) {
			et6B14turcos.setText((obj2.getP6_2_14_1() < 10 ? "0" : "")
					+ obj2.getP6_2_14_1());
		}
		if (obj2.getP6_2_14_2() != 0) {
			et6B14letrinas.setText((obj2.getP6_2_14_2() < 10 ? "0" : "")
					+ obj2.getP6_2_14_2());
		}
		if (obj2.getP6_2_14_3() != 0) {
			et6B14inodorosa1.setText((obj2.getP6_2_14_3() < 10 ? "0" : "")
					+ obj2.getP6_2_14_3());
		}
		if (obj2.getP6_2_14_4() != 0) {
			et6B14inodorosa2.setText((obj2.getP6_2_14_4() < 10 ? "0" : "")
					+ obj2.getP6_2_14_4());
		}
		if (obj2.getP6_2_14_5() != 0) {
			et6B14inodorosn1.setText((obj2.getP6_2_14_5() < 10 ? "0" : "")
					+ obj2.getP6_2_14_5());
		}
		if (obj2.getP6_2_14_6() != 0) {
			et6B14inodorosn2.setText((obj2.getP6_2_14_6() < 10 ? "0" : "")
					+ obj2.getP6_2_14_6());
		}
		if (obj2.getP6_2_14a() != 0) {
			et6B14Atotal.setText((obj2.getP6_2_14a() < 10 ? "0" : "")
					+ obj2.getP6_2_14a());
		}
		if (obj2.getP6_2_14b_1() != 0) {
			et6B141.setText((obj2.getP6_2_14b_1() < 10 ? "0" : "")
					+ obj2.getP6_2_14b_1());
		}
		if (obj2.getP6_2_14b_2() != 0) {
			et6B142.setText((obj2.getP6_2_14b_2() < 10 ? "0" : "")
					+ obj2.getP6_2_14b_2());
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.menu_capitulovi, menu);
		menu.getItem(0).setTitle("AMBIENTES POR PISO");
		menu.getItem(1).setTitle("SIGUIENTE");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub

		switch (item.getItemId()) {
		case R.id.mnSalir:
			AlertDialog.Builder dialogo1 = new AlertDialog.Builder(this);
			dialogo1.setTitle("Importante");
			dialogo1.setMessage("Deseas Salir?");
			dialogo1.setIcon(android.R.drawable.ic_dialog_alert);
			dialogo1.setCancelable(false);
			dialogo1.setPositiveButton("Si",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							System.exit(0);
						}
					});

			dialogo1.setNegativeButton("No",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialogo1, int id) {
							return;
						}
					});
			dialogo1.show();
			break;
		case R.id.mnMarco:
			Intent irMarco = new Intent(this, Carga_Trabajo_Activity.class);
			irMarco.putExtras(getIntent().getExtras());
			startActivity(irMarco);
			finish();
			break;
		case R.id.mnIncio:
			Intent marco = new Intent(this,
					CapituloVI_SeccionB_Lista_Ambientes_Activity.class);
			marco.putExtras(getIntent().getExtras());
			startActivity(marco);
			finish();
			break;
		case R.id.mnSiguiente:
			// guardar();
			obj2.setP6_2_14_1(et6B14turcos.getText().toString().length() == 0 ? 0
					: Integer.parseInt(et6B14turcos.getText().toString()));
			obj2.setP6_2_14_2(et6B14letrinas.getText().toString().length() == 0 ? 0
					: Integer.parseInt(et6B14letrinas.getText().toString()));
			obj2.setP6_2_14_3(et6B14inodorosa1.getText().toString().length() == 0 ? 0
					: Integer.parseInt(et6B14inodorosa1.getText().toString()));
			obj2.setP6_2_14_4(et6B14inodorosa2.getText().toString().length() == 0 ? 0
					: Integer.parseInt(et6B14inodorosa2.getText().toString()));
			obj2.setP6_2_14_5(et6B14inodorosn1.getText().toString().length() == 0 ? 0
					: Integer.parseInt(et6B14inodorosn1.getText().toString()));
			obj2.setP6_2_14_6(et6B14inodorosn2.getText().toString().length() == 0 ? 0
					: Integer.parseInt(et6B14inodorosn2.getText().toString()));
			obj2.setP6_2_14a(et6B14Atotal.getText().toString().length() == 0 ? 0
					: Integer.parseInt(et6B14Atotal.getText().toString()));
			obj2.setP6_2_14b_1(et6B141.getText().toString().length() == 0 ? 0
					: Integer.parseInt(et6B141.getText().toString()));
			obj2.setP6_2_14b_2(et6B142.getText().toString().length() == 0 ? 0
					: Integer.parseInt(et6B142.getText().toString()));
			cap6dao.actualizarSeccionB(obj2, lObj2);

			Intent irEvaluacion = new Intent(
					CapituloVI_SeccionB_Evaluacion_De_Los_SSHH_Activity.this,
					CapituloVI_SeccionB_Evaluacion_Del_Estado_De_Conservacion_Activity.class);
			irEvaluacion.putExtras(getIntent().getExtras());
			startActivity(irEvaluacion);

			finish();
			break;
		case R.id.mnFoto:
			Intent foto = new Intent(CapituloVI_SeccionB_Evaluacion_De_Los_SSHH_Activity.this,
					CapituloIX_Activity.class);
			// b.putBoolean("pregunta", true);
			foto.putExtras(getIntent().getExtras());
			foto.putExtra("pregunta", true);
			startActivity(foto);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		Intent irPredio = new Intent(this,
				CapituloVI_SeccionB_Lista_Ambientes_Activity.class);
		irPredio.putExtras(getIntent().getExtras());
		startActivity(irPredio);
		finish();
	}
}
